package com.ruicar.afs.cloud.apply.pre.loan.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruicar.afs.cloud.apply.common.entity.ApplyOprRecord;
import com.ruicar.afs.cloud.apply.pre.loan.condition.OrderOprCondition;
import com.ruicar.afs.cloud.apply.pre.loan.mapper.ApplyOprRecordMapper;
import com.ruicar.afs.cloud.apply.pre.loan.service.ApplyOprRecordService;
import com.ruicar.afs.cloud.common.core.security.util.SecurityUtils;
import com.ruicar.afs.cloud.common.core.security.vo.RoleVo;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @description: 订单操作记录服务类
 * @author: sijun.yu
 * @date: 2020/5/20 11:04
 */
@Service
public class ApplyOprRecordServiceImpl extends ServiceImpl<ApplyOprRecordMapper, ApplyOprRecord> implements ApplyOprRecordService {

    /**
     * 多条件查询操作记录信息
     *
     * @param condition
     * @param page
     * @return
     */
    @Override
    public IPage<List<ApplyOprRecord>> getOprRecord(OrderOprCondition condition, Page page) {
        return this.baseMapper.getOprRecord(condition, page);
    }


    @Override
    public IPage<ApplyOprRecord> searchOprInfoForAssertChange(OrderOprCondition condition, Page page) {
        // 用户名
        condition.setCreateBy(SecurityUtils.getUsername());
        JSONArray jsonArray = SecurityUtils.getUser().getUserExtInfo().getJSONArray("deptIds");
        if (jsonArray != null) {
            // 部门列表
            condition.setDeptIds(jsonArray.toJavaList(String.class));
        }
        JSONArray roleArray = SecurityUtils.getUser().getUserExtInfo().getJSONArray("roles");
        if (roleArray != null) {
            List<RoleVo> roleList = roleArray.toJavaList(RoleVo.class);
            condition.setRoleList(roleList);
        }
        return this.baseMapper.searchOprInfoForAssertChange(condition, page);
    }

    @Override
    public IPage<ApplyOprRecord> searchOprInfoForReconsider(OrderOprCondition condition, Page page) {
        // 用户名
        condition.setCreateBy(SecurityUtils.getUsername());
        JSONArray jsonArray = SecurityUtils.getUser().getUserExtInfo().getJSONArray("deptIds");
        if (jsonArray != null) {
            // 部门列表
            condition.setDeptIds(jsonArray.toJavaList(String.class));
        }
        JSONArray roleArray = SecurityUtils.getUser().getUserExtInfo().getJSONArray("roles");
        if (roleArray != null) {
            List<RoleVo> roleList = roleArray.toJavaList(RoleVo.class);
            condition.setRoleList(roleList);
        }
        return this.baseMapper.searchOprInfoForReconsider(condition, page);
    }

    /**
     * 更新操作记录信息
     *
     * @param condition
     * @return
     */
    @Override
    public Boolean updateOprRecord(OrderOprCondition condition) {
        return this.baseMapper.updateOprRecord(condition);
    }

    /**
     * 更新del_flag
     *
     * @param condition
     * @return
     */
    @Override
    public Boolean updateDelFlag(OrderOprCondition condition) {
        return this.baseMapper.updateDelFlag(condition);
    }

    @Override
    public List<ApplyOprRecord> oprInfoForReconsider(String channelId,String status) {
        JSONArray jsonArray = SecurityUtils.getUser().getUserExtInfo().getJSONArray("deptIds");
        List<String> deptIds = null;
        List<RoleVo> roleList=null;
        if(jsonArray!=null){
            // 部门列表
            deptIds=jsonArray.toJavaList(String.class);
        }
        JSONArray roleArray = SecurityUtils.getUser().getUserExtInfo().getJSONArray("roles");
        if(roleArray != null){
            roleList = roleArray.toJavaList(RoleVo.class);
        }

        List<ApplyOprRecord> applyOprRecords=this.baseMapper.getOprRecordByConditon(channelId,status,SecurityUtils.getUsername(),deptIds,roleList);
        return applyOprRecords;
    }
}
